package com.example.webdriveddd.controller;

import com.example.webdriveddd.application.Application;
import com.example.webdriveddd.application.NotFoundException;
import com.example.webdriveddd.dto.ShareInfoDTO;
import com.example.webdriveddd.repository.FileRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ShareViewController {
    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private Application application;
    @ModelAttribute
    public void user(Model model){
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        model.addAttribute("user",authentication.getName());
    }
    @GetMapping("/sharePage")
    public ModelAndView shareView(@RequestParam("fileId")int fileId) throws NotFoundException {
        ModelAndView modelAndView = new ModelAndView();
        ShareInfoDTO shareInfoDTO=application.accessFile(fileId);
        modelAndView.addObject("shareInfo", shareInfoDTO);
        modelAndView.setViewName("share");
        return modelAndView;
    }
}
